package cn.nexd.map.coreLocator;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.Map;

import cn.nexd.map.location.utils.Log;

/**
 * Created by admin on 15/8/30.
 */
public class coreWifiExtractor {
    private String[] wifilist;

    public void setWifiList(String[] wifilist){
        this.wifilist = wifilist.clone();
    }

    public Double[] extractWifi(String wifi_input) throws JSONException {

        Double[] test = new Double[this.wifilist.length];

        for(int i=0;i<test.length;i++){
            test[i] = 0.0;
        }

        JSONObject jsonObject = new JSONObject(wifi_input);
        Iterator<String> keyIter = jsonObject.keys();
        Double sum = 0.0;
        while (keyIter.hasNext()) {
            String keyStr = keyIter.next();
            String value = jsonObject.getString(keyStr);
//            Log.d("Debug_round1","Wifi Extraction "+keyStr + " with value: "+value);
            int index = _search_in_array(this.wifilist, keyStr);
            if(index != -1) {
                test[index] = Double.parseDouble(value) + 100.0;
                sum += test[index];
            }
        }
        Log.d("Debug_round1","WifiExtraction" + sum);
        return test;
    }

    public Double[] extractWifiWithRef(String wifi_input, String[] wifilist) throws JSONException, JSONException {

        Double[] test = new Double[wifilist.length];

        for(int i=0;i<test.length;i++){
            test[i] = 0.0;
        }

        JSONObject jsonObject = new JSONObject(wifi_input);
        Iterator<String> keyIter = jsonObject.keys();
        while (keyIter.hasNext()) {
            String keyStr = keyIter.next();
            String value = jsonObject.getString(keyStr);
//            Log.d("Debug_round1","Wifi Extraction "+keyStr + " with value: "+value);
            int index = _search_in_array(wifilist, keyStr);
            if(index != -1) {
                test[index] = Double.parseDouble(value) + 100.0;
            }
        }
        return test;
    }

    private int _search_in_array(String[] list, String target){
        int ind = -1;
        for(int i = 0; i < list.length; i++){
            if (list[i].equals(target)){
                ind = i;
                return ind;
            }
        }
        return ind;
    }
}
